package com.wcgksoft.controller.alarm;


import com.google.gson.JsonObject;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.Alert.Alarm;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.alarm.AlarmService;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 告警统计
 *
 * @author Administrator
 */
@Controller
@RequestMapping("alarm")
public class AlarmController {

    @Autowired
    private AlarmService alarmService;

    /**
     * 列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Alarm.class);
        criteria.add(Restrictions.eq("level", "高级"));
       SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        criteria.createAlias("departProjectData", "d");
        criteria.createAlias("d.equipment", "s");
        criteria.add(Restrictions.eq("s.e_id", sysUser.getEnterprise().getId()));
        List<Alarm> list = alarmService.findByConditions(criteria, page, limit);
        return Layui.data(0, "查询成功", alarmService.getTotalCount(), list);
    }
    /**
     * 年月
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value="all")
    public MsgInfo yr(){
        MsgInfo msgInfo = new MsgInfo();
//        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        JsonObject test=new JsonObject();
        String year = "";
        SimpleDateFormat sdfy = new SimpleDateFormat("yyyy");
        Date dd2 = Calendar.getInstance().getTime();
        year = sdfy.format(dd2);

        // 下标当月每天
       int[] mon={1,2,3,4,5,6,7,8,9,10,11,12};

        // 转换成天yyyy-MM-dd格式
        String aDate="";
        String gjsl="";
        String zjsl="";
        String zcsl="";
        for (int i = 1; i < mon.length + 1; i++) {
            int timeg; int timez; int timec;
            if (i < 10) {
                aDate = year + "-0" + i;
            } else {
                aDate = year + "-" + i;
            }
            //高级
            DetachedCriteria criteria = DetachedCriteria.forClass(Alarm.class);
            criteria.add(Restrictions.like("alarmtime",aDate, MatchMode.ANYWHERE));
            criteria.add(Restrictions.eq("level", "高级"));
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
            criteria.createAlias("departProjectData", "d");
            criteria.createAlias("d.equipment", "s");
            criteria.add(Restrictions.eq("s.e_id", sysUser.getEnterprise().getId()));
            List<Alarm> listg = alarmService.findByConditions(criteria);

            if(listg.size()>0){
               timeg=listg.size();
            }else{
                timeg=0;
            }
            gjsl += "," + timeg;

            //中级
            DetachedCriteria criteriaz = DetachedCriteria.forClass(Alarm.class);
            criteriaz.createAlias("departProjectData", "d");
            criteriaz.createAlias("d.equipment", "s");
            criteriaz.add(Restrictions.eq("s.e_id", sysUser.getEnterprise().getId()));
            criteriaz.add(Restrictions.like("alarmtime",aDate, MatchMode.ANYWHERE));
            criteriaz.add(Restrictions.eq("level", "中级"));
            List<Alarm> listz = alarmService.findByConditions(criteriaz);

            if(listz.size()>0){
                timez=listz.size();
            }else{
                timez=0;
            }
            zjsl += "," + timez;


            //正常
            DetachedCriteria criteriac = DetachedCriteria.forClass(Alarm.class);
            criteriac.createAlias("departProjectData", "d");
            criteriac.createAlias("d.equipment", "s");
            criteriac.add(Restrictions.eq("s.e_id", sysUser.getEnterprise().getId()));
            criteriac.add(Restrictions.like("alarmtime",aDate, MatchMode.ANYWHERE));
            criteriac.add(Restrictions.eq("level", "正常"));
            List<Alarm> listc = alarmService.findByConditions(criteriac);

            if(listc.size()>0){
                timec=listc.size();
            }else{
                timec=0;
            }
            zcsl += "," + timec;
        }

        gjsl = gjsl.replaceFirst(",", "");
        gjsl = "[" + gjsl + "]";
        msgInfo.setData(gjsl);

        zjsl = zjsl.replaceFirst(",", "");
        zjsl = "[" + zjsl + "]";
        msgInfo.setDatas(zjsl);

        zcsl = zcsl.replaceFirst(",", "");
        zcsl = "[" + zcsl + "]";
        msgInfo.setData1(zcsl);

        return msgInfo;
    }





}
